package com.example.springmvcdemo2;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/response")
@Controller
public class ResponseController {
    @RequestMapping("/getstatus")
    @ResponseBody
    public String getStatus() {
        return "这返回的是一个数据";
    }

    @RequestMapping("/r1")
    public Object r1() {
        return "redirect:/index.html";
    }

    @RequestMapping(value = "/returnjson")
    @ResponseBody
    public Map<String, String> returnJson() {
        Map<String, String> map = new HashMap<>();
        map.put("java", "java value");
        map.put("python", "python value");
        return map;
    }

    @RequestMapping("/index")
    public Object index() {
        return "/index.html";
    }

    @RequestMapping("/returnhtml")
    @ResponseBody
    public String returnHtml() {
        return "<h1>Hello,HTML~</h1>";
    }

    @RequestMapping("/returnstatus")
    @ResponseBody
    public String returnStatus(HttpServletResponse response) {
        response.setStatus(401);
        return "设置状态码成功";
    }

    @RequestMapping("/setheader")
    @ResponseBody
    public String setHeader(HttpServletResponse response) {
        response.setHeader("myheader", "666");
        return "设置header成功";
    }

    @RequestMapping(value = "/returnjson2")
    @ResponseBody
    public String returnJson2() {
        return "{\"sucess\":true}";
    }
}
